package com.shortlink.controller;

import com.shortlink.pojos.dto.dataobject.PageResult;
import com.shortlink.pojos.dto.dataobject.ResponseResult;
import com.shortlink.pojos.dto.req.LinkPageQueryReqDTO;
import com.shortlink.pojos.dto.req.LinkCreateReqDTO;
import com.shortlink.pojos.dto.req.LinkUpdateReqDTO;
import com.shortlink.pojos.dto.req.LinkStatsReqDTO;
import com.shortlink.pojos.dto.resp.LinkRespDTO;
import com.shortlink.pojos.dto.resp.LinkStatsRespDTO;
import com.shortlink.service.ILinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shortlink")
@Api(tags = "短链接管理模块")
public class ShortLinkController {
    @Autowired
    ILinkService iLinkService;

    @PostMapping("/create")
    @ApiOperation("创建短链接")
    public ResponseResult create(@RequestBody @Validated LinkCreateReqDTO linkCreateReqDTO){
        iLinkService.createLink(linkCreateReqDTO);
        return ResponseResult.okResult();
    }

    /**
     * 修改短链接
     * 包括启用禁用
     */
    @PutMapping("/update")
    @ApiOperation("修改短链接")
    public ResponseResult update(@RequestBody LinkUpdateReqDTO linkUpdateReqDTO){
        iLinkService.updateLink(linkUpdateReqDTO);
        return ResponseResult.okResult();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询短链接")
    public ResponseResult<PageResult<LinkRespDTO>> page(LinkPageQueryReqDTO linkPageQueryReqDTO){
        PageResult<LinkRespDTO> pageResult=iLinkService.pageLink(linkPageQueryReqDTO);
        return ResponseResult.okResult(pageResult);
    }

    /**
     * 逻辑删除
     * enable不变
     * @return
     */
    @PutMapping("/delete/{shortUri}")
    @ApiOperation("删除短链接")
    public ResponseResult delete(@PathVariable String shortUri){
        iLinkService.deleteLink(shortUri);
        return ResponseResult.okResult();
    }

    /**
     * 得到网站默认描述
     * @param url
     * @return
     */
    @GetMapping("/description/{url}")
    @ApiOperation("默认描述")
    public ResponseResult<String> description(@PathVariable String url){
        String description = iLinkService.getDescriptionByUrl(url);
        return ResponseResult.okResult(description);
    }

    /**
     * 得到网站默认图标
     * @param url
     * @return
     */
    @GetMapping("/favicon/{url}")
    @ApiOperation("默认图标")
    public ResponseResult<String> favicon(@PathVariable String url){
        String favicon = iLinkService.getFaviconByUrl(url);
        return ResponseResult.okResult(favicon);
    }

    /**
     * 访问单个短链接指定时间内监控数据
     * @param requestParam
     * @return
     */
    @GetMapping("/stats")
    @ApiOperation("监控数据")
    public ResponseResult<LinkStatsRespDTO> monitoringData(LinkStatsReqDTO requestParam) {
        LinkStatsRespDTO res=iLinkService.monitoringData(requestParam);
        return ResponseResult.okResult(res);
    }


}
